#Author: Mike Schladt - 2014
#Collection of methods to perform static analysis

import time
import pefile
import sets
import lib.mipreport as report
try :
    import cPickle as pickle
except :
    import pickle 


#function to kickoff the static anlaysis and reports
#returns an array of tag hashes
def start(sample):
    #tags set to eliminate duplicates
    tags = set()

    #find the complile time
    try :
        compile_time = get_compile_time(sample.file)
        tag = {}
        tag['type'] = 'compile_time'
        tag['value'] = compile_time
        tag['description'] = '[COMPILE TIME %Y-%m-%d %H:%M:%S]'
        tags.add(pickle.dumps(tag))    
    except Exception as e:
        print e
        #pass #probably not a pe file 

    #send tags to data base
    report.store_tags(sample.sid, tags)

def get_compile_time(pe_file):
    pe = pefile.PE(pe_file)
    epoch = pe.FILE_HEADER.TimeDateStamp
    humantime = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(epoch))
    return humantime

